import { wxRedirectTo } from '@/utils/wxApi'
import store from '../store/index'
import Fly from 'flyio/dist/npm/wx'
const fly = new Fly()
// 请求地址
// 超时时间
// let host = 'https://health.shuoshijiankang.com/api2'
// let host = 'http://192.168.200.103:8180'
// let host = 'https://health-dev.shuoshijiankang.com/api2'
// let host = 'https://health-fat.shuoshijiankang.com/api2'
// let host = 'https://health-uat.shuoshijiankang.com/api2'
console.log(process.env)
let host = `${process.env.VUE_APP_WX_BASE_URL}/api2`
fly.config.baseURL = host
fly.config.timeout = 80000
fly.interceptors.request.use((request) => {
  let flag1 = request.url.indexOf('wx/inspection/order') === -1
  let flag2 = request.url.indexOf('patient/updateMyPatient') === -1
  let flag3 = request.url.indexOf('patient/user/editPhone') === -1
  let flag4 = request.url.indexOf('/applyRefund') !== -1
  let flag5 = request.url.indexOf('/patient/address/editMyHisUserAddress') === -1
  let flag6 = request.url.indexOf('/sale/user/coupon/receive') != -1 || request.url.indexOf('/sale/user/coupon/exchange') != -1
  let flag7 = request.url.indexOf('/ssh-api/hisPregnancyPatientConfig/edit') === -1
  let flag8 = request.url.indexOf('/ssh-api/hisPregnancyPhysicalRecord/edit') === -1
  let contentType = request.method === 'GET' || (request.method === 'PUT' && flag2 && flag3 && flag5 && flag1 && flag7 && flag8) || flag4 || flag6 ? 'application/x-www-form-urlencoded' : 'application/json'
  let obj = {}
  if (store.getters && store.getters.healthToken && request.url.indexOf('sendSmsCode') === -1) {
    obj = {
      'Authorization': store.getters.healthToken
    }
  }
  if (store.getters && store.getters.cluster) {
    obj = Object.assign(obj, { 'X-CLUSTER': store.getters.cluster })
  }
  request.headers = {
    'content-type': contentType,
    'appType': 'WeChat',
    'userType': 'user',
    'apiVersion': '37',
    "apkVersion": '1.37.0'
  }
  request.headers = Object.assign(request.headers, obj)
  request.body && Object.keys(request.body).forEach((val) => {
    if (request.body[val] === '' && flag2) {
      delete request.body[val]
    }
  })
  request.body = {
    ...request.body
  }
  // 跨域请求是否发送第三方cookie
  request.withCredentials = true
  return request
})
// 添加响应拦截器
fly.interceptors.response.use(response => {
  if (response.data.resultStatus.code === 401 || response.data.resultStatus.code === 403) {
    store.commit('setToken', '')
    store.commit('setQuery', store.getters.currentPage)
    if (store.getters.isSdkReady) {
      uni.$TUIKit.logout()
    }
    // 判断是否授权，跳转不同的页面
    wxRedirectTo('/pages/login/authorize/index')
  }
  return response.data
}, err => {
  console.log('err___________________', err)
  if (err) {
    console.log('请求失败')
    return err
  }
})
export function uploadFileApiOss(host, formData, img) {
  return new Promise((resolve, reject) => {
    wx.uploadFile({
      url: `${host}`,
      filePath: img,
      formData,
      name: 'file',
      success(res) {
        resolve(res)
      },
      file(err) {
        console.log('err----', err)
        reject(err)
      }
    })
  })
}
// 下载文件
export function downloadFileApi(url, busPath) {
  return new Promise((resolve, reject) => {
    let savePath = `${wx.env.USER_DATA_PATH}/电子处方.pdf`
    wx.downloadFile({
      url: `${host}${url}?busPath=${busPath}`,
      filePath: savePath,
      header: {
        'Content-Type': 'application/json',
        'Authorization': store.getters.healthToken,
        'appType': 'WeChat',
        'userType': 'user'
      },
      success(res) {
        resolve(res)
      },
      file(err) {
        reject(err)
      }
    })
  })
}
export function getImgUrl(url) {
  return `${host}/ssh-api/im/head/img?path=${url}`
}

export default fly
